package com.aispeech.astools.recorder;


import android.hardware.usb.UsbRequest;
import android.util.Log;
import com.aispeech.astools.recorder.RecordListener.EVENT;
import java.lang.Thread.State;
import java.nio.ByteBuffer;

/**
 * @author rain  05.15 2019
 */

public class InterruptRecord extends HidRecord {
    private final String TAG = "InterruptRecord";

    public InterruptRecord(int vid, int pid) {
        mVid = vid;
        mPid = pid;
    }

    private Thread mInterruptReadThread = new Thread() {
        @Override
        public void run() {
            boolean retQueue = false;
            ByteBuffer byteBuffer = null;

            try {
                int inMax;
                synchronized (InterruptRecord.this) {
                    if (null == mUsbDeviceConnection || null == mUsbEndpointIn) {
                        Log.e(TAG, "urqReadRunnable,mUsbRequest mUsbDeviceConnection or mUsbEndpointIn is null ");
                        mListener.onError("mUsbDeviceConnection or mUsbEndpointIn is null");
                        return;
                    }

                    inMax = mUsbEndpointIn.getMaxPacketSize();
                    Log.d(TAG, "urqReadRunnable: inMax = " + inMax);
                    byteBuffer = ByteBuffer.allocate(inMax);
//                byteBuffer = ByteBuffer.allocateDirect(inMax);
                    mUsbRequest = new UsbRequest();

                    boolean ret = mUsbRequest.initialize(mUsbDeviceConnection, mUsbEndpointIn);
                    if (!ret) {
                        Log.e(TAG, "urqReadRunnable,mUsbRequest initialize err");
                        mListener.onError("mUsbRequest initialize err");
                        return;
                    }
                    isWorking = true;
                }

//                int waitTimes = 500;
                Log.d(TAG, "mInterruptReadThread run: start working ----------------------" + Thread.currentThread().getName() + " : " + Thread.currentThread().getId());
                Log.e(TAG, "start working: " + mUsbRequest.getEndpoint());
                while (isWorking) {
                    retQueue = mUsbRequest.queue(byteBuffer, inMax);

                    if (retQueue && (null != mUsbDeviceConnection) && (mUsbDeviceConnection.requestWait() == mUsbRequest)) {
                        if (isReading) {
                            mListener.onBuffer(byteBuffer.array());
                        }
                    } else {
                        Log.e(TAG, "read runnable: error ");
                        mListener.onError("the retQueue is false or mUsbDeviceConnection.requestWait not equals mUsbRequest, retQueue = " + retQueue);
                        break;
                    }
                }

                if (null != mUsbRequest) {
                    mUsbRequest.cancel();
                    mUsbRequest.close();
                    mUsbRequest = null;
                }

                if (null != mUsbDeviceConnection) {
                    mUsbDeviceConnection.close();
                    mUsbDeviceConnection = null;
                }
            } catch (Exception e) {
//                isReading = false;
                Log.e(TAG, "run: " + retQueue);
                Log.e(TAG, "run: " + mUsbDeviceConnection);
                Log.e(TAG, "run: " + byteBuffer);
                Log.e(TAG, "run: " + mUsbRequest);
                e.printStackTrace();
                mListener.onError(e.toString());
            }

            Log.d(TAG, "mInterruptReadThread run: stop working ----------------------" + Thread.currentThread().getName() + " : " + Thread.currentThread().getId());
            isWorking = false;
            isReading = false;
        }
    };

    @Override
    boolean startReadThread() {
        if (mInterruptReadThread.getState().equals(State.NEW) | mInterruptReadThread.getState().equals(State.TERMINATED)) {
            mListener.onEvent(EVENT.INIT);
            mInterruptReadThread.start();
            return true;
        }
        return false;
    }
}
